package com.margo.project.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * http://localhost:8080/api/swagger-ui.html#
 * @author fangxingbang
 *
 */
@Configuration
@EnableSwagger2
public class MargoSwaggerConfig {
	
	@Value("${margo.swagger.enable}")
	private boolean swaggerEnable;
	
	@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnable) //开关，默认为true，生产环境要改为false
                .tags(new Tag("validator", "JSR-303规范，Bean Validation"),getTags())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.margo.project"))
                .paths(PathSelectors.any())
                .build();
    }
    //配置在线文档的基本信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("margo-spring-web项目 api文档")
                .description("利用swagger构建api文档")
                //.termsOfServiceUrl("http://10.10.43.115:8081")
                .version("1.0")
                .build();
    }
    
    private Tag[] getTags() {
        Tag[] tags = {
            new Tag("RestTemplat", "RestTemplat客户端请求的Api"),
            new Tag("Spring-Session", "Spring Session相关的Api")
        };
        return tags;
    }

}
